//
//  AppDelegate.m
//  YTwallet
//
//  Created by hadrlog on 2019/2/22.
//  Copyright © 2019年 hadrlog. All rights reserved.
//

#import "AppDelegate.h"
#import "TXVagueManager.h"
#import "CoopenViewController.h"
#import "WXApi.h"
@interface AppDelegate ()<WXApiDelegate>

@property(nonatomic,strong)NSDate *startDate;
@property(nonatomic,strong)NSDate *endDate;

@property (strong, nonatomic)UIVisualEffectView *visualEffectView;


@end

@implementation AppDelegate


- (UIVisualEffectView *)visualEffectView{
    
    if (!_visualEffectView) {
        
       UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
        
        _visualEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
        
         _visualEffectView.frame = [UIScreen mainScreen].bounds;
        _visualEffectView.alpha = 1;
        
       }
    
     return _visualEffectView;
    
}


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [WXApi registerApp:@"wx0e122154e92091f1"];
    //打开APP先跳转开屏页
    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    self.window.rootViewController = [[CoopenViewController alloc] init];
    
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    //程序将要挂起
}
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
    return  [WXApi handleOpenURL:url delegate:self];
}

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
    return [WXApi handleOpenURL:url delegate:self];
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    //APP进入后端
    self.startDate = [NSDate date];
    
    /*
    self.endDate = [NSDate date];
    NSTimeInterval time = [self.endDate timeIntervalSinceDate:self.startDate];
    NSLog(@"间隔时间:%f",time);
    //    if (time > 60 * 5) {
    if (time > 5) {
        CoopenViewController *coopVC = [[CoopenViewController alloc] init];
        [[self findCurrentViewController] presentViewController:coopVC animated:YES completion:nil];
    }
    
    //返回前端,重新复制初始时间
    self.startDate = [NSDate date];
     */
    //延时300秒加遮盖解锁页面
    
    /*
     [[UIApplication sharedApplication].keyWindow addSubview:self.visualEffectView];
    */
    //打开模糊效果
    [[TXVagueManager vagueManager] open];
}

- (UIViewController *)findCurrentViewController
{
    UIWindow *window = [[UIApplication sharedApplication].delegate window];
    UIViewController *topViewController = [window rootViewController];
    
    while (true) {
        
        if (topViewController.presentedViewController) {
            
            topViewController = topViewController.presentedViewController;
            
        } else if ([topViewController isKindOfClass:[UINavigationController class]] && [(UINavigationController*)topViewController topViewController]) {
            
            topViewController = [(UINavigationController *)topViewController topViewController];
            
        } else if ([topViewController isKindOfClass:[UITabBarController class]]) {
            
            UITabBarController *tab = (UITabBarController *)topViewController;
            topViewController = tab.selectedViewController;
            
        } else {
            break;
        }
    }
    return topViewController;
}



- (void)applicationWillEnterForeground:(UIApplication *)application {
    
    //将要返回前台
    /*
    CoopenViewController *coopVC = [[CoopenViewController alloc] init];
    [coopVC setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
    [[self findCurrentViewController] presentViewController:coopVC animated:YES completion:nil];
     */
    
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    
    //回到前台
    /*
    if (self.visualEffectView) {
        [self.visualEffectView removeFromSuperview];
       }
     */
    //关闭模糊效果
    [[TXVagueManager vagueManager] close];

}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    // Saves changes in the application's managed object context before the application terminates.
    [self saveContext];
}


#pragma mark - Core Data stack

@synthesize persistentContainer = _persistentContainer;

- (NSPersistentContainer *)persistentContainer {
    // The persistent container for the application. This implementation creates and returns a container, having loaded the store for the application to it.
    @synchronized (self) {
        if (_persistentContainer == nil) {
            _persistentContainer = [[NSPersistentContainer alloc] initWithName:@"YTwallet"];
            [_persistentContainer loadPersistentStoresWithCompletionHandler:^(NSPersistentStoreDescription *storeDescription, NSError *error) {
                if (error != nil) {
                    // Replace this implementation with code to handle the error appropriately.
                    // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
                    
                    /*
                     Typical reasons for an error here include:
                     * The parent directory does not exist, cannot be created, or disallows writing.
                     * The persistent store is not accessible, due to permissions or data protection when the device is locked.
                     * The device is out of space.
                     * The store could not be migrated to the current model version.
                     Check the error message to determine what the actual problem was.
                    */
                    NSLog(@"Unresolved error %@, %@", error, error.userInfo);
                    abort();
                }
            }];
        }
    }
    
    return _persistentContainer;
}

#pragma mark - Core Data Saving support

- (void)saveContext {
    NSManagedObjectContext *context = self.persistentContainer.viewContext;
    NSError *error = nil;
    if ([context hasChanges] && ![context save:&error]) {
        // Replace this implementation with code to handle the error appropriately.
        // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
        NSLog(@"Unresolved error %@, %@", error, error.userInfo);
        abort();
    }
}

@end
